package com.work.intelligent_transportation2.fragment.CarSystem;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.work.intelligent_transportation2.R;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ETC_Account extends AppCompatActivity {

    private TextView etcBalance;
    private Spinner etcCarNumber;
    private EditText etcChargeMoney;
    private Button etcSearch;
    private Button etcCharge;
    private String host = "http://121.9.253.237:8888/traffic/api/v2/";
    private String pathGet = host + "get_car_account_balance.do";
    private String pathSet = host + "set_car_account_recharge.do";
    OkHttpClient okHttpClient = new OkHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_etc_account);
        initView();
        getBalanceByOkHttp(1);

        etcSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int carId = Integer.valueOf(etcCarNumber.getSelectedItem().toString());
                getBalanceByOkHttp(carId);
            }
        });
        etcCharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int carId = Integer.valueOf(etcCarNumber.getSelectedItem().toString());
                int money = Integer.valueOf(etcChargeMoney.getText().toString());
                rechargeByOkhttp(carId, money);
                getBalanceByOkHttp(carId);
            }
        });

    }

    private void rechargeByOkhttp(int carId, int money) {
        final ProgressDialog progressDialog = ProgressDialog.show(ETC_Account.this,
                "请稍等...", "账户充值中...", true);
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("UserName", "user1");
        jsonObject.addProperty("Money", money);
        jsonObject.addProperty("CarId", carId);
        String json = jsonObject.toString();
        Log.i("json", json);
        RequestBody requestBody = RequestBody.create(mediaType, json);
        final Request request = new Request.Builder()
                .url(pathSet).post(requestBody).build();
        final Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) { }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String json = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        Result result = gson.fromJson(json, Result.class);
                        if (result.getERRMSG().equals("成功")){
                            Toast.makeText(ETC_Account.this, "充值成功", Toast.LENGTH_LONG).show();
                        }
                        progressDialog.dismiss();
                    }
                });
                Log.i("json", json);
            }
        });
    }

    private void getBalanceByOkHttp(int carId) {
        final ProgressDialog progressDialog = ProgressDialog.show(ETC_Account.this,
                "请稍等...", "获取数据中...", true);
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("UserName", "user1");
        jsonObject.addProperty("CarId", carId);
        String json = jsonObject.toString();
        Log.i("json", json);
        RequestBody requestBody = RequestBody.create(mediaType, json);
        Request request = new Request.Builder()
                .url(pathGet).post(requestBody).build();
        final Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) { }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        BalanceBean balanceBean = gson.fromJson(json, BalanceBean.class);
                        etcBalance.setText(String.valueOf(balanceBean.getBalance()));
                        progressDialog.dismiss();
                    }
                });
                Log.i("json", json);
            }
        });
    }

    private void initView() {
        etcBalance = (TextView) findViewById(R.id.etc_balance);
        etcCarNumber = (Spinner) findViewById(R.id.etc_CarNumber);
        etcChargeMoney = (EditText) findViewById(R.id.etc_ChargeMoney);
        etcSearch = (Button) findViewById(R.id.etc_search);
        etcCharge = (Button) findViewById(R.id.etc_charge);
    }
}